package com.artPreter.service.impl;

import com.artPreter.mapper.*;
import com.artPreter.pojo.*;
import com.artPreter.service.IArticleService;
import com.artPreter.service.IArtworkService;
import com.artPreter.service.IPictureService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.artPreter.pojo.dto.ArtistName;
import com.artPreter.pojo.dto.ItemBase;
import com.artPreter.service.IArtistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

import static com.artPreter.utils.PinyinUtil.getPinyin;

/**
 * <p>
 * 艺术家表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-14
 */
@Service
public class ArtistServiceImpl extends ServiceImpl<ArtistMapper, Artist> implements IArtistService {

    @Autowired
    private ContentArtistRelativeMapper contentArtistRelativeMapper;
    @Autowired
    private IArtworkService artworkService;
    @Autowired
    private IPictureService pictureService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private ArtistSectionTempMapper artistSectionTempMapper;
    @Autowired
    private ArtistRelativeMapper artistRelativeMapper;
    @Autowired
    private ArtistContentSectionMapper artistContentSectionMapper;
    @Autowired
    private ArtistTempMapper artistTempMapper;


    @Override
    public IPage<Artist> getPage(Integer currentPage, Integer size, Artist artist) {
        // 创建分页对象
        IPage<Artist> page = new Page<>(currentPage, size);
        // 构建查询条件
        LambdaQueryWrapper<Artist> queryWrapper = new LambdaQueryWrapper<>();
        // 排序
        queryWrapper.orderByDesc(Artist::getSort);
        queryWrapper.eq(Artist::getStatus,1);
        // 名字模糊查询
        if(artist.getName()!=null&&!"".equals(artist.getName())){
            queryWrapper.like(Artist::getName,artist.getName());
        }
        // artistid
        if(artist.getArtistId()!=null&&!"".equals(artist.getArtistId())){
            queryWrapper.eq(Artist::getArtistId,artist.getArtistId());
        }


        // 调用分页查询方法
        return this.baseMapper.getPage(page, queryWrapper);
    }

    @Override
    public void add(Artist artist) {
        artist.setName(artist.getName().replaceAll("\\s", "")) ;
        String pinyin = getPinyin(artist.getName());
        if(!pinyin.isEmpty()){
            artist.setPinyin(pinyin);
            String pinyinFirst = pinyin.substring(0,1);
            artist.setFirstPy(pinyinFirst);
        }
        artist.setStatus(1);
        // 生成UUID并设置到实体对象中
        String uuid = UUID.randomUUID().toString();
        artist.setArtistId(uuid);
        this.baseMapper.insert(artist);
    }

    @Override
    public void edit(Artist artist) {
        artist.setName(artist.getName().replaceAll("\\s", ""));
        String pinyin = getPinyin(artist.getName());
        if(!pinyin.isEmpty()){
            artist.setPinyin(pinyin);
            String pinyinFirst = pinyin.substring(0,1);
            artist.setFirstPy(pinyinFirst);
        }
        this.baseMapper.updateById(artist);
    }

    @Override
    public void delete(Artist artist) {
        if(artist.getId()==null){
            return;
        }
        LambdaQueryWrapper<Artist> artistLambdaQueryWrapper = new LambdaQueryWrapper<>();
        artistLambdaQueryWrapper.eq(Artist::getId,artist.getId());
        List<Artist> artistList = this.baseMapper.selectList(artistLambdaQueryWrapper);
        if(artistList.size()>0){
            if(artistList.get(0).getArtistId()!=null&&!"".equals(artistList.get(0).getArtistId())){
                String artistId = artistList.get(0).getArtistId();
                // 删除艺术家关联数据
                LambdaQueryWrapper<ContentArtistRelative> contentArtistRelativeLambdaQueryWrapper = new LambdaQueryWrapper<>();
                contentArtistRelativeLambdaQueryWrapper.eq(ContentArtistRelative::getArtistId,artistId);
                contentArtistRelativeMapper.delete(contentArtistRelativeLambdaQueryWrapper);

                // 删除艺术家关联艺术品
                artworkService.deleteByArtistId(artistId);
                // 删除艺术家关联文章
                articleService.deleteByArtistId(artistId);
                // 删除艺术家关联图片
                pictureService.deleteByArtistId(artistId);
                // 删除艺术家栏目表
                LambdaQueryWrapper<ArtistSectionTemp> artistSectionTempLambdaQueryWrapper = new LambdaQueryWrapper<>();
                artistSectionTempLambdaQueryWrapper.eq(ArtistSectionTemp::getArtistId,artistId);
                artistSectionTempMapper.delete(artistSectionTempLambdaQueryWrapper);
                // 删除艺术机构关联艺术家表
                LambdaQueryWrapper<ArtistRelative> artistRelativeLambdaQueryWrapper = new LambdaQueryWrapper<>();
                artistRelativeLambdaQueryWrapper.eq(ArtistRelative::getArtistId,artistId);
                artistRelativeMapper.delete(artistRelativeLambdaQueryWrapper);
                // 删除艺术家栏目内容表
                LambdaQueryWrapper<ArtistContentSection> artistContentSectionLambdaQueryWrapper = new LambdaQueryWrapper<>();
                artistContentSectionLambdaQueryWrapper.eq(ArtistContentSection::getArtistId,artistId);
                artistContentSectionMapper.delete(artistContentSectionLambdaQueryWrapper);
                // 删除艺术家模板内容表
                LambdaQueryWrapper<ArtistTemp> artistTempLambdaQueryWrapper = new LambdaQueryWrapper<>();
                artistTempLambdaQueryWrapper.eq(ArtistTemp::getArtistId,artistId);
                artistTempMapper.delete(artistTempLambdaQueryWrapper);


            }
            this.baseMapper.deleteById(artistList.get(0).getId());
        }

        artist.setStatus(0);
        this.baseMapper.updateById(artist);
    }

    @Override
    public IPage<Artist> getPageWithDepart(Integer currentPage, Integer size, Artist artist) {
        //        "WHERE d.name LIKE '%百%' AND ar.status = 1 ;"
        QueryWrapper<Artist> queryWrapper = new QueryWrapper<>();
        // 艺术家名字
        if(artist.getName()!=null&&!"".equals(artist.getName())){
            queryWrapper.like("a.name",artist.getName());
        }
        System.out.println(artist.getDepartName());
        // 艺术机构名字
        if(artist.getDepartName()!=null&&!"".equals(artist.getDepartName())){
            System.out.println(artist.getDepartName());
            queryWrapper.like("d.name",artist.getDepartName());
            queryWrapper.eq("ar.status",1);
        }
        IPage<Artist> artistIPage = new Page<>(currentPage,size);
        artistIPage = this.baseMapper.getPageWithDepart(artistIPage,queryWrapper);

        return artistIPage;
    }


    @Override
    public List<ArtistName> getListByFirstPy(Artist artist) {
        QueryWrapper<Artist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<ItemBase> list = this.baseMapper.getListByFirstPy(queryWrapper);
        String[] alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
        List<ArtistName> result = new java.util.ArrayList<>();
        for (String s : alphabet) {
            ArtistName artistName = new ArtistName();
            artistName.setFirstPy(s);
            List<ItemBase> list1 = new java.util.ArrayList<>();
            for (ItemBase itemBase : list) {
                if(s.equals(itemBase.getFirstPy())){
                    list1.add(itemBase);
                }
            }
            artistName.setData(list1);
            result.add(artistName);
        }
        return  result;
    }

    @Override
    public void addToRelative(Artist artist) {
        ContentArtistRelative contentArtistRelative = new ContentArtistRelative();
        if(artist.getArtistId()!=null&&!"".equals(artist.getArtistId())){
            contentArtistRelative.setArtistId(artist.getArtistId());
        }else {
            return;
        }
        if(artist.getContentId()!=null&&!"".equals(artist.getContentId())){
            contentArtistRelative.setContentId(artist.getContentId());
        }else {
            return;
        }
        LambdaQueryWrapper<ContentArtistRelative> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ContentArtistRelative::getContentId,artist.getContentId());
        queryWrapper.eq(ContentArtistRelative::getArtistId,artist.getArtistId());
        if(contentArtistRelativeMapper.selectCount(queryWrapper)>0){
            return;
        }else{
            contentArtistRelativeMapper.insert(contentArtistRelative);
        }
    }

    @Override
    public void saveDepartRelativeSort(List<Artist> artistList) {
        // jv_content_artist_relative
        for (int i = 0; i < artistList.size(); i++) {
            if(artistList.get(i).getArtistId()!=null&&!artistList.get(i).getArtistId().equals("")&&artistList.get(i).getContentId()!=null&&!artistList.get(i).getContentId().equals("")){
                QueryWrapper<ContentArtistRelative> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("content_id",artistList.get(i).getContentId());
                System.out.println(artistList.get(i).getArtistId()+"artistList.get(i).getArtistId()");
                queryWrapper.eq("artist_id",artistList.get(i).getArtistId());
                ContentArtistRelative contentArtistRelative = new ContentArtistRelative();
                if(artistList.get(i).getCtSort()==null){
                    contentArtistRelative.setSort(0);
                }else{
                    contentArtistRelative.setSort(artistList.get(i).getCtSort());
                }
                contentArtistRelativeMapper.update(contentArtistRelative,queryWrapper);
            }
        }

    }

    @Override
    public Artist getInfo(Artist artist) {
        LambdaQueryWrapper<Artist> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(artist.getId()!=null||artist.getArtistId()!=null){
            if(artist.getArtistId()!=null){
                lambdaQueryWrapper.eq(Artist::getArtistId,artist.getArtistId());
            }else {
                lambdaQueryWrapper.eq(Artist::getId,artist.getId());
            }
            List<Artist> list = this.baseMapper.selectList(lambdaQueryWrapper);
            if(list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    @Override
    public IPage<Artist> getPageRelative(Integer currentPage, Integer size, Artist artist) {
//        LambdaQueryWrapper<Artist> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Artist::getStatus,1);
//        queryWrapper.orderByDesc(Artist::getId);
//        String departId = artist.getDepartId();
//        // 内容id
//        String contentId = artist.getContentId();
//        queryWrapper.exists("SELECT 1 FROM jv_content_artist_relative WHERE jv_content_artist_relative.content_id = '"+contentId+"' and jv_content_artist_relative.artist_id = jv_artist.artist_id");
//        queryWrapper.exists("SELECT 1 FROM jv_artist_relative WHERE jv_artist_relative.artist_id = jv_artist.artist_id and jv_artist_relative.depart_id = '"+departId+"' and jv_artist_relative.status = 1");
//        IPage<Artist> artistIPage = new Page<>(currentPage,size);
//        // 艺术家名字模糊搜索
//        if(artist.getName()!=null&&!"".equals(artist.getName())){
//            queryWrapper.like(Artist::getName,artist.getName());
//        }
//
//        artistIPage = this.baseMapper.selectPage(artistIPage, queryWrapper);

        IPage<Artist> artistIPage = new Page<>(currentPage,size);
        QueryWrapper<Artist> artistQueryWrapper = new QueryWrapper<>();
        artistQueryWrapper.eq("s.content_id",artist.getContentId());
        // 艺术家名字模糊搜索
        if(artist.getName()!=null&&!artist.getName().equals("")&&!artist.getName().equals("null")){
            artistQueryWrapper.like("b.name",artist.getName());
            System.out.println("模糊搜索名字");
        }
        // 排序
        artistQueryWrapper.orderByDesc("s.sort");
        artistIPage = this.baseMapper.getRelatedArtistPageList(artistIPage,artistQueryWrapper);
        return artistIPage;
    }

    @Override
    public IPage<Artist> getPageNoRelative(Integer currentPage, Integer size, Artist artist) {
        LambdaQueryWrapper<Artist> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Artist::getStatus,1);
        queryWrapper.orderByDesc(Artist::getId);
        String departId = artist.getDepartId();
        // 内容id
        String contentId = artist.getContentId();
        queryWrapper.notExists("SELECT 1 FROM jv_content_artist_relative WHERE jv_content_artist_relative.content_id = '"+contentId+"' and jv_content_artist_relative.artist_id = jv_artist.artist_id");
        queryWrapper.exists("SELECT 1 FROM jv_artist_relative WHERE jv_artist_relative.artist_id = jv_artist.artist_id and jv_artist_relative.depart_id = '"+departId+"' and jv_artist_relative.status = 1");
        IPage<Artist> artistIPage = new Page<>(currentPage,size);
        // 艺术家名字模糊搜索
        if(artist.getName()!=null&&!"".equals(artist.getName())){
            queryWrapper.like(Artist::getName,artist.getName());
        }

        artistIPage = this.baseMapper.selectPage(artistIPage, queryWrapper);
        return artistIPage;
    }

    @Override
    public void removeArtistRelative(Artist artist) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(artist.getArtistId()!=null&&!"".equals(artist.getArtistId())){
            queryWrapper.eq("artist_id",artist.getArtistId());
        }else {
            return;
        }
        if(artist.getContentId()!=null&&!"".equals(artist.getContentId())){
            queryWrapper.eq("content_id",artist.getContentId());
        }else {
            return;
        }
        contentArtistRelativeMapper.delete(queryWrapper);
    }
}
